Python Scripting
Python packages are managed from the Admin console under Scripting Environments. Simply select the required environment from the relevant drop down in the Properties panel; click the Packages button see which packages have been downloaded to the given environment.
Configure the Python Scripting Node
Script
There are four ways in which you can provide the Python script:
- Generate Your Script: get an AI-generated Python script based on a given prompt (purple arrow below).
- Marketplace: download a script from the Pyramid Marketplace (red arrow below). Once downloaded, the script will appear in the script window.
- Pick a Script: open the content manager folder tree to select a script that was built and saved in Pyramid (green arrow below). Once the script is selected, it will appear in the script window.
- Write or Paste a Script: write or paste a script directly into the script window (blue highlight below).
Script Type
You can select a regular script, or a learn and predict script (orange highlight below). Learn and predict scripts are trained on a given data set, and can then be used to make predictions.
- Click here to learn about learn and predict scripts.
Environment
Choose the virtual Python environment that uses the required Python version and packages (yellow highlight above).
Pyramid enables Admins to create multiple virtual environments, where each of these environments can use a different Python version and different 3rd party packages. Pyramid supports Python environments up to and including Python 3.10
Packages
View the list of packages that have been downloaded to the currently selected virtual Python environment.
- Click here to learn about virtual scripting environments.
Script Window
Any script that is downloaded, generates using AI, injected from a shared script, or written or pasted is written in the Script window (blue highlight above).
Use AI to Generate a Script
Pyramid's Generative AI integration lets you use AI to generate Python statements. This is useful if you want to generate code quickly, or don't know the syntax, for instance, To do this, click the Gen AI icon from the Properties panel. In the text field at the top of the Generate Your Python dialog, enter a description of the query you want to perform.
The dialog for generating your script contains the following fields and buttons:
- Text Field: enter a description of the query you want to perform and click the arrow to enter your query description and return a it as a script.
- Script Window: the AI-generated script will appear in the scripting window.
- Explain Code: open an AI-generated explanation of the script that was returned in the scripting window.
- Apply: apply the script to the Query node.
- Cancel: close the dialog without applying any changes.
Explain Code
|
Use the Explain Script function to produce an AI-generated explanation of what the script does. Each time you click the Explain Script button, a new explanation is generated. This explanation can be generated regardless of the method used to generate the script itself. For more information, see Explain Script. |
This explanation can be copied, and then pasted to the "Description" field, for instance.
Warning: AI-generated assets are generated from public domain algorithms, which can produce both erroneous and inconsistent or random results. Use at your own risk.
In this example, the Python scripting node was connected to the fact table:
AI was used to generate a script to replace nulls with zeros:
Opening the Explain Script dialog, we can see an explanation of the AI-generated Python script:
The Sales column was given as the input, and the output added to the existing table:
When the Python node is previewed, we see the new column at the end of the table:
Inputs and Outputs
The input window is used to configure the column(s) that will be injected into the script. The output window is used to configure the new column(s) that will be produced by the script. You can also determine whether the new column(s) will be added to the existing table (the table to which the Python node is connected), or stored in a new a table.
When you download a script from the Marketplace, Pyramid automatically detects the inputs and outputs. When writing a script of choosing a shared script, you'll need to configure the input and output columns yourself. You also have the option to use to let Pyramid auto detect the output from the script.
- Click here to learn more about scripting inputs and outputs.
- Click here to learn more about the auto detect function.
Preview
Click the preview icon from the script properties to load run the script and preview the results in the Preview panel. Any errors will be displayed in the Error panel.
In this example, the Python script 'Round Numbers' was downloaded from the Marketplace and used to round numbers in the Overhead column.
Upon downloading, it was automatically added to the Script window:
The downloaded script is as follows. Depending on the script, some objects can be changed manually if required. For instance, in this example the "precision" object determines the number of decimal places that will be returned. This is set to 2, but can be changed. If you want the column's values rounded to whole numbers, change the precision to 0.
# Inputs: # ------- # numbers: A numeric column # The number of digits after the dot (the desired precision) precision = 2 rounded=[] for i in range(0,len(numbers)): curr = numbers[i] if curr is None or str(curr).strip()=='': rounded.append(0) else: rounded.append(round(float(numbers[i]),precision)) outputDF = pa.DataFrame({'rounded': rounded})
The Input and Output columns are loaded automatically when a script is downloaded from the Marketplace. However, you may need to edit the Input column(s) manually to ensure the correct columns are selected.
In this example, the OverHead column is the input, and the output is a new column called 'rounded', added to the existing table.
When the script is previewed, we see the table in the Preview panel. The original Overhead column (blue highlight below) is retained, and the new 'rounded' column is added (green highlight below):
In this example, an eye-ware company wants to use DBSCAN clustering to cluster distances of their customers to each other.
They want to group data points together based on two conditions: when their distance from the other data points in the cluster does not exceed 0.3 km; and when there are at least 10 data points in the cluster.
import pandas as pa from sklearn.cluster import DBSCAN X = pa.DataFrame(a,a1) db = DBSCAN(eps=0.3, min_samples=10).fit(X) b=db.labels_